package ch06_container.list;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 
 *
 * @author guod
 * @version 1.0 use jdk 1.8
 */
public class CreArrayList {
    // 创建list集合对象
    @Test
    public void createList() {
        List<Integer> list01 = new ArrayList<>();
        List<Integer> list02 = new ArrayList<>() {
            {
                add(1);
                add(2);
            }
        };
        List<Integer> list03 = Arrays.asList(1, 2, 3, 4);
        List<Integer> list04 = Stream.of(1, 2, 3).collect(Collectors.toList());
        List<Integer> list05 = Lists.newArrayList(1, 2);
        // 9之后
        List<Integer> list06 = List.of(1, 2, 3);

        System.out.println(list04);
        System.out.println(list05);
    }

    // list转数组
    @Test
    public void list2Array() {
        List<Integer> list01 = Arrays.asList(1, 2, 3, 4);
        Integer[] arrays01 = list01.toArray(new Integer[0]);
        Integer[] arrays02 = list01.toArray(Integer[]::new);
        Integer[] arrays03 = Convert.toIntArray(list01);
    }

    // 数组转list
    @Test
    public void array2List() {
        Integer[] aa = new Integer[1];
        aa[0] = 1;
        // Integer[] aa = {1, 2};
        List<Integer> list01 = Arrays.asList(aa);
        List<Integer> list02 = List.of(aa);
        List<Integer> list03 = Convert.toList(Integer.class, aa);
        List<Integer> list04 = CollUtil.toList(aa);
    }

    // 集合的交集、差集
    @Test
    public void listOpt() {
        List<Integer> aa = Arrays.asList(0, 1, 2, 3);
        List<Integer> bb = Arrays.asList(1, 2, 3, 9, 8, 7);
        System.out.println(CollUtil.subtractToList(aa, bb));
        System.out.println(CollUtil.intersection(aa, bb));
    }

    @Test
    public void arraysOpt() {
        Long[] aa = {1L, 2L};
        Long[] bb = {1L, 2L, 3L};
        CollUtil.subtract(CollUtil.toList(aa), Convert.toList(Long.class, bb));
    }
}
